int Left_motor_back=5;     //左电机后退(IN1)
int Left_motor_go=6;     //左电机前进(IN2)

int Right_motor_go=9;    // 右电机前进(IN3)
int Right_motor_back=10;    // 右电机后退(IN4)

void setup()
{
  //初始化电机驱动IO为输出方式
  pinMode(Left_motor_go,OUTPUT); // PIN 5 (PWM)
  pinMode(Left_motor_back,OUTPUT); // PIN 6 (PWM)
  pinMode(Right_motor_go,OUTPUT);// PIN 9 (PWM) 
  pinMode(Right_motor_back,OUTPUT);// PIN 10 (PWM)
}
void run(int time)     // 前进
{
  analogWrite(Right_motor_go,0);//右电机前进，PWM比例0~255调速，左右轮差异略增减
  analogWrite(Right_motor_back,200);
  analogWrite(Left_motor_go,200);// 左电机前进，PWM比例0~255调速，左右轮差异略增减
  analogWrite(Left_motor_back,0);
  delay(time * 100);   //执行时间，可以调整  
}

void brake(int time)         //刹车，停车
{
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
  delay(time * 100);//执行时间，可以调整  
}

void left(int time)         //左转(左轮不动，右轮前进)
{
  analogWrite(Right_motor_go,0); //右电机前进，PWM比例0~255调速
  analogWrite(Right_motor_back,200);
  digitalWrite(Left_motor_go,LOW);   //左轮不动
  digitalWrite(Left_motor_back,LOW);
  delay(time * 100);    //执行时间，可以调整  
}

void spin_left(int time)         //左转(左轮后退，右轮前进)
{
  analogWrite(Right_motor_go,0); //右电机前进，PWM比例0~255调速
  analogWrite(Right_motor_back,200);

  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,200);//左轮后退PWM比例0~255调速
  delay(time * 100);    //执行时间，可以调整  
}

void right(int time)        //右转(右轮不动，左轮前进)
{
  digitalWrite(Right_motor_go,LOW);   //右电机不动
  digitalWrite(Right_motor_back,LOW);

  analogWrite(Left_motor_go,200); 
  analogWrite(Left_motor_back,0);//左电机前进，PWM比例0~255调速
  delay(time * 100);    //执行时间，可以调整  
}

void spin_right(int time)        //右转(右轮后退，左轮前进)
{
  analogWrite(Right_motor_go,200); 
  analogWrite(Right_motor_back,0);//右电机后退，PWM比例0~255调速

  analogWrite(Left_motor_go,200); //左电机前进，PWM比例0~255调速
  analogWrite(Left_motor_back,0);
  delay(time * 100);    //执行时间，可以调整  
}

void back(int time)          //后退
{
  analogWrite(Right_motor_go,150);
  analogWrite(Right_motor_back,0);//右轮后退，PWM比例0~255调速

  analogWrite(Left_motor_go,0);
  analogWrite(Left_motor_back,150);//左轮后退，PWM比例0~255调速
  delay(time * 100);     //执行时间，可以调整  
}

void loop()
{
  delay(1000); //延时1s后启动
  run(40);//前进8s
  right(8);//向右转1s
  run(20);//前进4s
  right(8);//向右转1s

  run(40);//前进8s
  right(8);//向右转1s
  run(20);//前进4s
  right(8);//向右转1s
}